package com.cloud.xtools.common.controller;

import com.cloud.xtools.common.response.ApiResponse;
import com.cloud.xtools.common.response.PageList;
import com.cloud.xtools.common.response.ResponseBody;
import com.cloud.xtools.common.response.ResponseCode;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Daixinguo
 */
public class BaseController {

    @Autowired
    private HttpServletRequest request;

    protected Long userId(){
        Object userId = request.getAttribute("user_id");
        if (userId instanceof Integer) {
            return ((Integer) userId).longValue();
        } else {
            return null;
        }
    }

    protected String username(){
        Object username = request.getAttribute("username");
        if (username instanceof String) {
            return (String) username;
        } else {
            return null;
        }
    }

    protected <T> ApiResponse<T> success() {
        ResponseCode responseCode =ResponseCode.SUCCESS;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), responseCode.getMessage());
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> success(String message) {
        ResponseCode responseCode =ResponseCode.SUCCESS;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), message);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> success(T data) {
        ResponseCode responseCode =ResponseCode.SUCCESS;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), responseCode.getMessage(), data);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> success(String message, T data) {
        ResponseCode responseCode =ResponseCode.SUCCESS;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), message, data);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    // 分页列表数据
    protected <T> ApiResponse<T> pageList(T data) {
        ResponseCode responseCode =ResponseCode.SUCCESS;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), responseCode.getMessage(), data);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    // 创建成功 201
    // 不返回创建成功的数据，使用默认提示信息
    protected <T> ApiResponse<T> created() {
        ResponseCode responseCode =ResponseCode.CREATED;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), responseCode.getMessage());
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> created(T data) {
        ResponseCode responseCode =ResponseCode.CREATED;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), responseCode.getMessage(), data);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> created(String message, T data) {
        ResponseCode responseCode =ResponseCode.CREATED;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), message, data);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    // 不返回创建成功的数据，只提示创建成功
    protected <T> ApiResponse<T> created(String message) {
        ResponseCode responseCode =ResponseCode.CREATED;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), message);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    // 无内容返回 204
    protected <T> ApiResponse<T> noContent() {
        ResponseCode responseCode =ResponseCode.NO_CONTENT;
        return new ApiResponse<>(null, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> error() {
        ResponseCode responseCode =ResponseCode.ERROR;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), responseCode.getMessage());
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }

    protected <T> ApiResponse<T> error(String message) {
        ResponseCode responseCode =ResponseCode.ERROR;
        ResponseBody<T> responseBody = new ResponseBody<>(responseCode.getCode(), message);
        return new ApiResponse<>(responseBody, responseCode.getHttpStatus());
    }
}
